package com.bit.whoareu;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DBMS {
	DBHelper dbhelper;
	SQLiteDatabase db;
	Context context;
	Cursor cursor;

	DBMS(Context context) {
		this.context = context;
		this.open();
	}

	public void open() {
		dbhelper = new DBHelper(context);

		try {
			db = dbhelper.getWritableDatabase();
		} catch (SQLiteException e) {
			db = dbhelper.getReadableDatabase();
		}
	}

	public boolean datecheck() {
		cursor = db.rawQuery("SELECT * FROM contact;", null);
		return cursor.moveToNext();
	}
	
	public int dateinput(int count, String email,String phone){
		int num = 0;
		 if (this.datecheck()){
             db.execSQL("update contact set count="+count+",email='"+email+"',phone='"+phone+"' where code='1'");
             num=1;
          }else{
             db.execSQL("insert into contact values ('1',"+count+",'"+email+"','"+phone+"')");
             num=1;
             }
		return num;
	}
	
	public InfoDTO dateselect() {
		InfoDTO info = null;
		cursor = db.rawQuery("SELECT * FROM contact;", null);
		
		if (cursor.moveToNext()) {
			int count = cursor.getInt(1);
			String email = cursor.getString(2);
			String phone = cursor.getString(3);
			info = new InfoDTO(count, email,phone);
		}
		return info;
	}
}
